package com.desay.pangoo.itmt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.desay.pangoo.common.lite.lang.web.BusinessException;
import com.desay.pangoo.common.lite.lang.web.CommonResponse;
import com.desay.pangoo.itmt.enums.ExceptionCodeEnum;
import com.desay.pangoo.itmt.service.WorkGroupService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 数据集
 *
 * @author uidq1163
 */
@RestController
@Api(tags = "ITMT工作组管理")
@RequestMapping(value = "/workgroup")
@Validated
public class WorkGroupController {
	@Autowired
	private WorkGroupService workGroupService;

	@PostMapping()
	@ApiOperation(value = "添加工作组", notes = "添加工作组")
	public ResponseEntity<Object> create(@RequestParam(value = "groupName") String groupName,
			@RequestParam(value = "ptnHeader") String ptnHeader) {
		Long id = workGroupService.create(groupName, ptnHeader);
		return new ResponseEntity<>(CommonResponse.success(id), HttpStatus.OK);
	}

	@DeleteMapping(value = "{id}")
	@ApiOperation(value = "删除", notes = "删除工作组")
	@ApiImplicitParam(name = "id", value = "工作组ID", required = true, dataTypeClass = Long.class, paramType = "path")
	public ResponseEntity<Object> delete(@PathVariable long id) {
		try {
			workGroupService.delete(id);
		} catch (DataIntegrityViolationException e) {
			throw new BusinessException(ExceptionCodeEnum.WORK_GROUP_EXIST_RELATION);
		}
		return new ResponseEntity<>(CommonResponse.success(), HttpStatus.OK);
	}

	@PutMapping(value = "{id}")
	@ApiOperation(value = "更新", notes = "更新工作组")
	public ResponseEntity<Object> update(@PathVariable(value = "id") Long id,
			@RequestParam(value = "groupName") String groupName) {
		workGroupService.update(id, groupName);
		return new ResponseEntity<>(CommonResponse.success(), HttpStatus.OK);
	}

	@GetMapping(value = "")
	@ApiOperation(value = "获取工作组列表", notes = "获取工作组列表")
	public ResponseEntity<Object> getAllList() {
		return new ResponseEntity<>(CommonResponse.success(workGroupService.getAll()), HttpStatus.OK);
	}
}
